package com.podcastellano.radiopodapp;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;

public class Listening {
	    private String titulo;
	    private String album;
	    private Bitmap imagen;
	 
	    public String getTitulo() {
	        return titulo;
	    }
	 
	    public String getAlbum() {
	        return album;
	    }
	 
	    public Bitmap getImagen() {
	        return imagen;
	    }
	   
	 
	    public void setTitulo(String t) {
	        titulo = t;
	    }
	 
	    public void setAlbum(String l) {
	        album = l;
	    }
	 
	    public void setImagen(String d) {
	    	
	    	String url;
	    	Integer i=0;
	    	do{
	    		url= "http://radio.podcastellano.org/now_playing-"+d;
	    		switch (i) {
	    		
	    		  case 0:
	    		        url+=".jpg";
	    		        break;
	    		  case 1: 
	    			    url+=".png";
	    		        break;
	    		  case 2:
	    			  url+=".gif";
	    		        break;
	    		  
	    		}
	    		try {
	    			imagen = ImageOperations(new URL(url));
				} catch (MalformedURLException e) {
					e.printStackTrace();
				}
	    		i++;
	    	}while(imagen==null&i<2);
	    	if(imagen==null){
	    		
	    		try {
	    			imagen = ImageOperations(new URL("http://radio.podcastellano.org/logo-radio.gif"));
				} catch (MalformedURLException e) {
					e.printStackTrace();
				}
	    	}
	    	
	    }
	    
	    private Bitmap ImageOperations(URL link) {
			try {
				InputStream is = link.openConnection().getInputStream();
				Bitmap d = BitmapFactory.decodeStream(is, null, null);
				return d;
			} catch (MalformedURLException e) {
				e.printStackTrace();
				return null;
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				return null;
			} catch (IOException e) {
				e.printStackTrace();
				return null;
			}
		}
	    public Object fetch(String address) throws MalformedURLException,IOException {
			URL url = new URL(address);
			Object content = url.getContent();
			return content;
		}
}
